package com.ph.shopping.facade.permission.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.ph.shopping.common.core.base.BaseService;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.mapper.UserMapper;
import com.ph.shopping.facade.mapper.UserRoleMapper;
import com.ph.shopping.facade.permission.entity.User;
import com.ph.shopping.facade.permission.service.ILoginService;
import com.ph.shopping.facade.permission.vo.SessionUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @项目：phshopping-service-permission
 * @描述：
 * @作者： Mr.zheng
 * @创建时间：2017-03-15
 * @Copyright @2017 by Mr.zheng
 */
@Component
@Service(version="1.0.0")
public class LoginService extends BaseService implements ILoginService {

    private static final Logger logger = LoggerFactory.getLogger(LoginService.class);

    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;

    /**
     * 登录校验
     * @param user
     * @return
     */
    @Override
    public Result login(User user) {
        logger.info("登录参数入参，user={}", JSON.toJSONString(user));
        try {
            User dbUser = userMapper.findUserByPhone(user.getTelphone());
            if (dbUser == null || !user.getPassword().equals(dbUser.getPassword())) {
                //手机号或者密码有错误
                return getResult(ResultEnum.TELPHONE_PASSWORD_ERROR);
            }
            if (dbUser.getIsable() == 2) {
            	return getResult(ResultEnum.USER_ISABLED);
            }
            SessionUserVo sessionUserVo = new SessionUserVo();
            sessionUserVo.setId(dbUser.getId());
            sessionUserVo.setTelphone(dbUser.getTelphone());
            sessionUserVo.setSessionRoleVo(userRoleMapper.selectRoleVoByUserID(dbUser.getId()));
            return getResult(ResultEnum.SUCCESS,sessionUserVo);
        } catch (Exception ex) {
            logger.error("登录异常,ex={}", ex);
        }
        return getResult(ResultEnum.INTERNAL_SERVER_ERROR);
    }
}
